#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#       config.py
#
#       Copyright (C) 2010  Catullus Cassius
#
#       This file is part of python-vkontakte-player.
#
#       This program is free software: you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation, either version 3 of the License, or
#       (at your option) any later version.
#
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#
#       You should have received a copy of the GNU General Public License
#       along with this program.  If not, see <http://www.gnu.org/licenses/>.

import shelve
import os

file_names = {u'config_dir': u'.pvp', 
u'session': u'session.db'}

def get_home_dir():
	if os.name == 'posix':
		return os.environ['HOME']
	elif os.name == 'nt':
		return os.environ['HOMEPATH']

def config_dir_check():
	config_dir_path = get_home_dir()+'/'+file_names['config_dir']
	if os.access(config_dir_path, os.F_OK):
		return True
	else:
		os.mkdir(config_dir_path)
		return False

def config_file_check(config_file_path, mode):
	if not config_dir_check():
		return False
	if os.access(config_file_path, mode):
		return True
	else:
		return False

def load_session():
	session_path = get_home_dir()+'/'+file_names['config_dir']+'/'+file_names['session']
	if config_file_check(session_path, os.R_OK):
		s = shelve.open(session_path, 'r')
		session = s['session']
		s.close()
		return session

def save_session(session):
	config_dir_check()
	session_path = get_home_dir()+'/'+file_names['config_dir']+'/'+file_names['session']
	s = shelve.open(session_path, 'c')
	s['session'] = session
	s.close()

